#!/usr/bin/perl -w
####################################################
# Copyright (c) 2002-2007 BioWare Corp.
# http://www.bigdaddymonitor.org
#
# This file is part of the BigDaddy Monitoring
# program. It is free software distributed under
# the terms of the BSD License. It is ditributed
# in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without event the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
# The BSD License is available from:
# http://www.opensource.org/licenses/bsd-license.php
####################################################
#=============================================================================#
#BigDaddy Module: Windows SNMP RAM Checker
#Author: Craig Miller
#Date: 19.10.2006
#=============================================================================#

=intro

=cut

use strict;
use POSIX qw(ceil);
my ($sServer) = @ARGV;

my @aResults=`snmpwalk -c public -v 1 $sServer HOST-RESOURCES-MIB::hrStorage`;

my $sSearch;
my @aStorage;
my $nStorageDisk;
my %hStorageAllocation;
my %hStorageSize;
my %hStorageUsed;

foreach my $sLine (@aResults)
{
    if ($sLine =~ m/hrStorageType.([0-9]+) = OID: HOST-RESOURCES-TYPES::hrStorage([A-Za-z]+)/i)
    {
       if (lc("Ram") eq lc($2))
        {
            $nStorageDisk=$1;
        }
    }
    
    if ($sLine =~ m/hrStorageAllocationUnits.([0-9]+) = INTEGER: ([0-9]+) Bytes/i)
    {
        $hStorageAllocation{$1}=$2;
    }
    
    if ($sLine =~ m/hrStorageSize.([0-9]+) = INTEGER: ([0-9]+)/i)
    {
        $hStorageSize{$1}=$2;
    }
    
    if ($sLine =~ m/hrStorageUsed.([0-9]+) = INTEGER: ([0-9]+)/i)
    {
        $hStorageUsed{$1}=$2;
    }
}

my $nDiskSize=$hStorageSize{$nStorageDisk}*$hStorageAllocation{$nStorageDisk};
my $nDiskUsage=$hStorageUsed{$nStorageDisk}*$hStorageAllocation{$nStorageDisk};
my $nPercentUsed=ceil(($nDiskUsage/$nDiskSize)*100);

print $nPercentUsed;
